package com.ccx.web.backand;

import com.ccx.model.EbUser;
import com.ccx.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/User")
public class userController {
    @Autowired
    private UserService  userService;
    //1.商品分页
    @RequestMapping("/to_save")
    private  String list(Model model,
                         @RequestParam(value = "page",defaultValue = "1")Integer page,
                         @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        System.out.println("进入User");
        List<EbUser> ebUsers = userService.list(page, pageSize);
        System.out.println(ebUsers);
        //2.指定model和view （将数据绑定视图中显示）
        model.addAttribute("ebUsers",ebUsers);//key-value
        //2.1 传递当前页到里
        model.addAttribute("curPage",page);
        //2.2 传递最大页到
        model.addAttribute("maxPage",((Page)ebUsers).getPages());

        return "manage/user";
    }



    @RequestMapping("/list")
             public  String toSave(){
                  return "manage/user-add";
                }


      @RequestMapping("/save")
    public String save(EbUser ebUser){
        ebUser.setEuStatus(1);
        System.out.println("ebUser==>" +ebUser);
        Integer result = userService.save(ebUser);
          if(result<1) {
              //添加失败
              return "manage/manage-error";
          }
          return "manage/manage-result";
      }


      @RequestMapping("/to_edit")
    public  String toEdit(Model model,@RequestParam("euUserId") String euUserId){
        EbUser ebUser = userService.selectOne(euUserId);
          model.addAttribute("ebUser",ebUser);
        return  "manage/user-modify";
      }

        @RequestMapping("/edit")
        public  String edit(EbUser ebUser){

            ebUser.setEuStatus(1);
            System.out.println("ebUser==>"+ebUser);

            Integer result = userService.updateUser(ebUser);
            System.out.println("result"+result);
            if(result<1){
                return "manage/manage-error";
            }
            return  "manage/manage-result";
        }

    @RequestMapping("/delete")
    public  String DeleteUser(EbUser ebUser){
        int i = userService.delete(ebUser);
        System.out.println(ebUser.getEuUserId());
            if(i<1){
                return "manage/manage-error";
            }
            return  "manage/manage-result";
        }
    //用户的注册功能
    @RequestMapping("/to_register")
    public String toregister(){
        return "register";//register.jsp
    }

    @RequestMapping("/register")
    public String register(EbUser ebUser) {
        ebUser.setEuStatus(1);
        System.out.println("ebUser==>" + ebUser);
        //1.调用service层的用户添加方法
        Integer result = userService.save(ebUser);

        //2.根据添加返回过来的结果决定跳转网页
        if (result < 1) {
            //添加失败的情况
            return "reg-result";
        }
        return "reg-result";
    }
}


